Download: Mobile Task Management
EdgerOS provides download function, and developers must use the API provided by Web-SDK to download.
Functions
edger.downloadTask.download(url, options[, headers])
Perform an http request to the server and download the file to the specified destination.
url
{String} Url.options
{Object}filename
{String} File name in download task.ext
{String} File suffix in download task.
headers
{Object}- Returns: {Promise} Fulfill upon success with an object.
The edger.downloadTask.download()
method returns an object.
The returned object will have the following properties:
id
{String} Task id, which can be used for functions edger.downloadTask.abort(id), edger.downloadTask.state(id), edger.downloadTask.retry(id) and edger.filesystem.open(id).status
{string} Status.
The current attribute has the following range of values:
waiting
{String} Waiting.downloading
{String} Downloading.success
{String} Success.failure
{String} Failure.
Example
const url = '/path';
const headers = {};
edger.downloadTask.download(url, { filename: 'filename', ext: 'ext' }, headers).then((data) => {
const { id, status } = data;
console.log('Current downloadTask information:', id, status);
}).catch(error => {
console.error(error);
});
async / await
async function download() {
try {
const url = '/path';
const headers = {};
return await edger.downloadTask.download(url, { filename: 'filename', ext: 'ext' }, headers);
} catch (error) {
console.error(error);
}
}
edger.downloadTask.abort(id)
Cancel the download.
id
{String} Task id.- Returns: {Promise} Promise object.
Example
const downloadTaskId = 'id';
edger.downloadTask.abort(downloadTaskId).then((data) => {
const { success } = data;
console.log('Current downloadTask abort information:', success);
}).catch(error => {
console.error(error);
});
async / await
async function abort() {
try {
const downloadTaskId = 'id';
return await edger.downloadTask.abort(downloadTaskId);
} catch (error) {
console.error(error);
}
}
edger.downloadTask.state(id)
Retrieve the current download task's status, progress, and other relevant information.
id
{String} Task id.- Returns: {Promise} Promise object.
Actively obtain the status of the current download task.
The returned object will have the following properties:
id
{String} Task id.progress
{Number} Progress, 0.00 ~ 100.00.status
{string} Status.
The current attribute has the following range of values:
waiting
{String} Waiting.downloading
{String} Downloading.success
{String} Success.failure
{String} Failure.
Example
const downloadTaskId = 'id';
edger.downloadTask.state(downloadTaskId).then((data) => {
const { id, progress, status } = data;
console.log('Current downloadTask information:', id, progress, status);
}).catch(error => {
console.error(error);
});
async / await
async function state() {
try {
const downloadTaskId = 'id';
return await edger.downloadTask.state(downloadTaskId);
} catch (error) {
console.error(error);
}
}
edger.downloadTask.retry(id)
Restart the download.
id
{String} Task id.- Returns: {Promise} Promise object.
The edger.downloadTask.retry()
method returns an object.
The returned object will have the following properties:
id
{String} Task id.status
{string} Status.
The current attribute has the following range of values:
waiting
{String} Waiting.downloading
{String} Downloading.success
{String} Success.failure
{String} Failure.
Example
const downloadTaskId = 'id';
edger.downloadTask.retry(downloadTaskId).then((data) => {
const { id, status } = data;
console.log('Current downloadTask information:', id, status);
}).catch(error => {
console.error(error);
});
async / await
async function retry() {
try {
const downloadTaskId = 'id';
return await edger.downloadTask.retry(downloadTaskId);
} catch (error) {
console.error(error);
}
}
Events
The unified event listener provided by Web-SDK:
const listener = (payload) => {
// Event handling...
}
// add listener
edger.addEventListener('some-event', listener);
// or
// onAction() is an alias of addEventListener().
edger.onAction('some-event', listener);
// remove listener
edger.removeEventListener('some-event', listener);
// remove all listeners
edger.removeAllListeners();
download
This event will be generated when download task in progress.
Example
const listener = (payload) => {
const { id, progress, status } = payload;
console.log('Current downloadTask information:', id, progress, status);
}
edger.addEventListener('download', listener);